* Set current directory and open log
cd "E:\Seagate Sync\VOL\My Documents\Research\Current projects\prlx\Posted\101113 EUP publication\Data analysis"
capture log close
log using "prlx-v01-analysis02", replace text


************************************************************************************
* Variables for start date, end date, and type of outcome of Council decision-making
************************************************************************************


* Project and author information
********************************
* Project:		prlx
* Programme:	prlx-v01-analysis02.do
* Date:			20 November 2010
* Author:		Frank Haege, University of Limerick
* Contact:		frank.haege@ul.ie 

* Do-file description
*********************
* This do-file restricts the dataset generated by "prlx-v01-management02.do" to binding legal instruments 
* adopted through the three main legislative procedures (consultation, cooperation, and codecision).  
* It generates variables for the start and end date, as well as the type of outcome of 
* Council decision-making.


* Clear memory  
**************
version 11.0
clear all
macro drop _all
set more off

* Load dataset
use "prlx-v01-management02.dta", clear
des, short


* Restrict dataset to binding legal instruments adopted through main legislative procedures
*******************************************************************************************

* Limit dataset to regulations, directives, and decisions
keep if instrument == 1 | instrument == 2 | instrument == 3

* Limit dataset to consultation, cooperation, and codecision files
keep if procedure == 1 | procedure == 2 | procedure == 3
tab instrument procedure, m


* Generate date of transmission to Council variable
***************************************************

* In the 1989 cases, transmission to Council occurs before adoption by Commission!
* This is formally impossible and therefore not picked up by the extraction script.
list webno procedure if datetrans == .
* Use date of adoption by Commission for missing transmission to Council cases (70 cases)
replace datetrans = dateadop if datetrans == .

* Drop existing year and month variables
drop year month

* Generate year variables for start of Council decision-making process
generate year = year(datetrans)
tab year, m
label var year "Start year"

* Generate month variable for start of Council decision-making process
generate month = month(datetrans)
tab month, m
label var month "Start month"


* Generate date variables for end of Council decision-making events
*******************************************************************
* If the same type of event took place several times, the date of the last time is used to code the variable
	
* Generate date variable for formal adoption by Council
codebook formAdopByCoun_date_*
generate cfadoptld = formAdopByCoun_date_0
forvalues i = 1/6 {
	replace cfadoptld = formAdopByCoun_date_`i' if formAdopByCoun_date_`i' ~= "NA"
}
generate cfadoptldd = date(cfadoptld, "DMY")
label var cfadoptldd "Date of formal adoption by Council"

* Generate date variable for Council agreement
codebook counAgre_date_*
generate cagreeld = counAgre_date_0
forvalues i = 1/2 {
	replace cagreeld = counAgre_date_`i' if counAgre_date_`i' ~= "NA"
}
generate cagreeldd = date(cagreeld, "DMY")
label var cagreeldd "Date of Council agreement"

* Generate date variable for Council political agreement on common position
codebook polAgreCommPosi_date_*
generate cpagreeld = polAgreCommPosi_date_0
forvalues i = 1/3 {
	replace cpagreeld = polAgreCommPosi_date_`i' if polAgreCommPosi_date_`i' ~= "NA"
}
generate cpagreeldd = date(cpagreeld, "DMY")
label var cpagreeldd "Date of political agreement Common Position"

* Generate date variable for Council adoption of common position
codebook adopCommPosi_date_*
generate cpaadoptld = adopCommPosi_date_0
forvalues i = 1/3 {
	replace cpaadoptld = adopCommPosi_date_`i' if adopCommPosi_date_`i' ~= "NA"
}
generate cpaadoptldd = date(cpaadoptld, "DMY")
label var cpaadoptldd "Date of adoption Common Position"

* Generate date variable for Council approval at first reading
codebook counAppr1stRdg_date_*
generate capprovalld = counAppr1stRdg_date_0
replace capprovalld = counAppr1stRdg_date_1 if counAppr1stRdg_date_1 ~= "NA"
generate capprovalldd = date(capprovalld, "DMY")
label var capprovalldd "Date of Council approval first reading"

* Generate date variable for Council approval at second reading
codebook counAppr2ndRdg_date_*
generate capproval2ld = counAppr2ndRdg_date_0
replace capproval2ld = counAppr2ndRdg_date_1 if counAppr2ndRdg_date_1 ~= "NA"
generate capproval2ldd = date(capproval2ld, "DMY")
label var capproval2ldd "Date of Council approval second reading"
* Correction of idiosyncratic mistake
* First reading decision was described as second reading decision in Prelex
replace capproval = 1 if webno == 182213 | webno == 182214
replace capproval2 = 0 if webno == 182213 | webno == 182214
replace capprovalld = capproval2ld if webno == 182213 | webno == 182214
replace capprovalldd = capproval2ldd if webno == 182213 | webno == 182214

* Generate date variable for Council approval at third reading
codebook counDeciAt3rdRdg_date_*
generate capproval3ld = counDeciAt3rdRdg_date_0
forvalues i = 1/2 {
	replace capproval3ld = counDeciAt3rdRdg_date_`i' if counDeciAt3rdRdg_date_`i' ~= "NA"
}
generate capproval3ldd = date(capproval3ld, "DMY")
label var capproval3ldd "Date of Council approval third reading"

* Generate date variable for Council partial adoption
codebook partAdopByCoun_date_*
generate cpartadoptld = partAdopByCoun_date_0
forvalues i = 1/4 {
	replace cpartadoptld = partAdopByCoun_date_`i' if partAdopByCoun_date_`i' ~= "NA"
}
generate cpartadoptldd = date(cpartadoptld, "DMY")
label var cpartadoptldd "Date of partial adoption by Council"

* Generate date variable for signature by EP and Council
codebook signByEpAndCoun_date_*
generate signatureld = signByEpAndCoun_date_0
replace signatureld = signByEpAndCoun_date_1 if signByEpAndCoun_date_1 ~= "NA"
generate signatureldd = date(signatureld, "DMY")
label var signatureldd "Date of signature by EP and Council"

* Generate date variable for partial signature by EP and Council
codebook partSignByEpAndCoun_date_*
generate partsignld = partSignByEpAndCoun_date_0
replace partsignld = partSignByEpAndCoun_date_1 if partSignByEpAndCoun_date_1 ~= "NA"
replace partsignld = partSignByEpAndCoun_date_2 if partSignByEpAndCoun_date_2 ~= "NA"
generate partsignldd = date(partsignld, "DMY")
label var partsignldd "Date of partial signature by EP and Council"

* Generate date variable for withdrawal by Commission
generate withdrawd = date(withByComm_date_0, "DMY")
label var withdrawd "Date of withdrawal by Commission"

* Generate date variable for replacement by Commission
generate repld = date(replacement_date_0, "DMY")
replace repld = date(replacement_date_1, "DMY") if replacement_date_1 ~= "NA"
label var repld "Date of replacement by Commission"


* Generate end date of Council decision-making variable
*******************************************************
generate cenddate = .
label var cenddate "End date"

* Codecision procedure
**********************
* First reading approval
tab procedure capproval, m
replace cenddate = capprovalldd if procedure == 3 & cenddate == .
* Adoption of common position 
tab procedure cpaadopt, m
replace cenddate = cpaadoptldd if procedure == 3 & cenddate == .
replace cenddate = cpaadoptldd if procedure == 3 & cenddate < cpaadoptldd & cpaadoptldd ~= .
* Formal adoption by Council
tab procedure cfadopt, m
replace cenddate = cfadoptldd if procedure == 3 & cenddate == .
* 30 changes
replace cenddate = cfadoptldd if procedure == 3 & cenddate < cfadoptldd & cfadoptldd ~= .
* No changes
* Partial adoption
replace cenddate = cpartadoptldd if procedure == 3 & cenddate == .
* Withdrawn by Commission
replace cenddate = withdrawd if procedure == 3 & cenddate == .
* Replacement by Commission
replace cenddate = repld if procedure == 3 & cenddate == .

* Consultation procedure
************************
* Formal adoption by Council
tab procedure cfadopt, m
replace cenddate = cfadoptldd if procedure == 1 & cenddate == .
* Council first reading approval
tab procedure capproval, m
replace cenddate = capprovalldd if procedure == 1 & cenddate == .
replace cenddate = capprovalldd if procedure == 1 & cenddate < capprovalldd & capprovalldd ~= .
* Partial adoption
replace cenddate = cpartadoptldd if procedure == 1 & cenddate == .
* Withdrawn by Commission
replace cenddate = withdrawd if procedure == 1 & cenddate == .
* Replacement by Commission
replace cenddate = repld if procedure == 1 & cenddate == .

* Cooperation procedure
***********************
* Adoption of common position
tab procedure cpaadopt, m
replace cenddate = cpaadoptldd if procedure == 2 & cenddate == .
* Partial adoption
replace cenddate = cpartadoptldd if procedure == 2 & cenddate == .
* Withdrawn by Commission
replace cenddate = withdrawd if procedure == 2 & cenddate == .
* Replacement by Commission
replace cenddate = repld if procedure == 2 & cenddate == .


* Generate Council decision-making outcome variable
***************************************************
generate cenddated = .
label var cenddated "Type of outcome"

* Codecision procedure
**********************
* First reading approval
replace cenddated = 1 if capproval == 1 & procedure == 3 & cenddated == . 
* Adoption common position
replace cenddated = 1 if cpaadopt == 1 & procedure == 3 & cenddated == .
* Formal adoption by Council
replace cenddated = 1 if cfadopt == 1 & procedure == 3 & cenddated == .
* Partial adoption
replace cenddated = 2 if cpartadopt == 1 & procedure == 3 & cenddated == .
* Withdrawn by Commission
replace cenddated = 3 if withdraw == 1 & procedure == 3 & cenddated == .
* Replacement by Commission
replace cenddated = 4 if repl == 1 & procedure == 3 & cenddated == .

* Consultation procedure
************************
* Formal adoption by Council
replace cenddated = 1 if cfadopt == 1 & procedure == 1 & cenddated == .
* First reading approval
replace cenddated = 1 if capproval == 1 & procedure == 1 & cenddated == .
* Partial adoption
replace cenddated = 2 if cpartadopt == 1 & procedure == 1 & cenddated == .
* Withdrawn by Commission
replace cenddated = 3 if withdraw == 1 & procedure == 1 & cenddated == .
* Replacement by Commission
replace cenddated = 4 if repl == 1 & procedure == 1 & cenddated == .

* Cooperation procedure
***********************
* Adoption of common position
replace cenddated = 1 if cpaadopt == 1 & procedure == 2 & cenddated == .
* Partial adoption
replace cenddated = 2 if cpartadopt == 1 & procedure == 2 & cenddated == .
* Withdrawn by Commission
replace cenddated = 3 if withdraw == 1 & procedure == 2 & cenddated == .
* Replacement by Commission
replace cenddated = 4 if repl == 1 & procedure == 2 & cenddated == .


* Check and correct inconsistencies
***********************************
* Most of them are 'correct', i.e. the inconsistencies are due to changes in the treaty base after introduction

* Check proposals that report formal adoption during cooperation procedure
tab procedure cfadopt if cenddated == ., m
list webno if cfadopt == 1 & cenddated == .
replace cenddated = 1 if cfadopt == 1 & cenddated == .
replace cenddate = cfadoptldd if cenddate == .

* Check proposals that report adoption of common position under consultation procedure
tab procedure cpaadopt if cenddated == ., m
list webno if cpaadopt == 1 & cenddated == .
replace cenddated = 1 if cpaadopt == 1 & cenddated == .
replace cenddate = cpaadoptldd if cenddate == .

* Check proposals that report first reading approval under cooperation procedure
tab procedure capproval if cenddated == ., m
list webno procedure if capproval == 1 & cenddated == .
replace cenddated = 1 if capproval == 1 & cenddated == .
replace cenddate = capprovalldd if cenddate == .

* Code pending proposals
tab cenddated, m
recode cenddated . = 5

* Define decision-making event label
label define cenddatedl 0 "None" 1 "Adoption" 2 "Partial adoption" 3 "Withdrawal" 4 "Replacement" 5 "Pending", modify

* Label decision-making outcome variable
label val cenddated cenddatedl
tab cenddated, m


* Drop irrelevant variables
drop cfadoptld cagreeld cpagreeld cpaadoptld capprovalld capproval2ld capproval3ld cpartadoptld /*
	*/ signatureld partsignld *_date_*

* Save data
compress
save "prlx-v01-analysis02.dta", replace

* Exit do-file
log close
exit
